/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var username = document.getElementById("username");
var fullname = document.getElementById("fullname");
var email = document.getElementById("email");
var currentEmail=email.value;
var password = document.getElementById("txtPassword");
var newPassword = document.getElementById("txtNewPassword");
var confirmPass = document.getElementById("txtConfirm");

var btUpdateInfo=document.getElementById("btUpdateInfo");
var btUpdatePassword=document.getElementById("btUpdatePassword");

var fullnameInfo=document.getElementById("fullnameInfo");
var emailInfo=document.getElementById("emailInfo");

var passwordInfo=document.getElementById("passwordInfo");
var newPasswordInfo=document.getElementById("newPasswordInfo");
var confirmInfo=document.getElementById("confirmInfo");
var successInfo = document.getElementById("successInfo");

var FULLNAME_REGEX=/^([a-zA-Z'-]+\s*){2,5}$/;
var PASSWORD_REGEX=/^.{6,16}$/; //Password từ 6 đến 16 char
var EMAIL_REGEX = /^(\w+[\-\.])*\w+@(\w+\.)+[A-Za-z]+$/; //viết theo thầy Khánh...

fullname.onblur=function(){
    checkFullname();
};
confirmPass.onblur=function(){
    checkConfirm();
};
email.onblur=function(){
    checkEmail();
};

btUpdateInfo.onclick=updateInfo;
btUpdatePassword.onclick=updatePassword;

function updateInfo(e){
    passwordInfo.innerHTML='';
    newPasswordInfo.innerHTML='';
    confirmInfo.innerHTML='';
    successInfo.innerHTML='';
        
    e.preventDefault();
    
    if (validateAccountInput()){
        request=new XMLHttpRequest();
        var url="updateAccount.action";
        var params = "fullname="+ fullname.value
        +"&email="+ email.value
        +"&currentEmail="+currentEmail;

        request.open("POST", url,true);
        request.onreadystatechange=updateInfoResult;
        request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        request.send(params);
    }

}

function updateInfoResult(){
    if (request.readyState==4) {
        if (request.status==200) {
            var result = eval('('+ request.responseText +')');
            if (result.message=='success') {
                successInfo.innerHTML=result.info.fontcolor("blue");
            } else if (result.message=='failureemail') {
                emailInfo.innerHTML=result.info.fontcolor("red");
            }
        }
    }
}

function updatePassword(e){
    passwordInfo.innerHTML='';
    fullnameInfo.innerHTML='';
    emailInfo.innerHTML='';
    successInfo.innerHTML='';
    
    e.preventDefault();
    
    if (validatePasswordInput()){
        request=new XMLHttpRequest();
        var url="updatePassword.action";
        var params = "username="+username.value
        +"&newPassword="+ newPassword.value
        +"&password="+ password.value;

        request.open("POST", url,true);
        request.onreadystatechange=updatePasswordResult;
        request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        request.send(params);
    }

}

function updatePasswordResult(){
    if (request.readyState==4) {
        if (request.status==200) {
            var result = eval('('+ request.responseText +')');
            if (result.message=='success') {
                successInfo.innerHTML=result.info.fontcolor("blue");
            } else if (result.message=='failure') {
                passwordInfo.innerHTML=result.info.fontcolor("red");
            }
        }
    }
}

function validateAccountInput(){
    var isValid=true;
    if (!checkFullname()) {
        isValid=false;
    }
    if (!checkEmail()) {
        isValid=false;
    }
    return isValid;
}

function validatePasswordInput(){
    var isValid=true;
    if (!checkNewPassword()) {
        isValid=false;
    }
    if (!checkConfirm()) {
        isValid=false;
    }
    return isValid;
}

function checkFullname(){
    fullnameInfo.innerHTML='';
    if (!FULLNAME_REGEX.test(fullname.value)) {
        fullnameInfo.innerHTML='Tên đầy đủ phải gồm ít nhất 2 phần và chỉ chứa ký tự.'.fontcolor("red");
        return false
    }
    return true
}

function checkNewPassword(){
    newPasswordInfo.innerHTML='';
    if (!PASSWORD_REGEX.test(newPassword.value)) {
        newPasswordInfo.innerHTML='Mật khẩu phải dài từ 6 đến 16 ký tự.'.fontcolor("red");
        return false
    }
    return true
}

function checkConfirm(){
    confirmInfo.innerHTML='';
    if (newPassword.value!=confirmPass.value) {
        confirmInfo.innerHTML='Không giống mật khẩu phía trên.'.fontcolor("red");
        return false
    }
    return true
}

function checkEmail(){
    emailInfo.innerHTML='';
    if (!EMAIL_REGEX.test(email.value)) {
        emailInfo.innerHTML='Email sai định dạng.'.fontcolor("red");
        return false
    }
    return true
}